package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.modules.shop.entity.GoodsCategory;
import cn.signalpha.modules.shop.service.GoodsCategoryService;
import cn.signalpha.modules.shop.service.GoodsService;
import cn.signalpha.modules.shop.vo.GoodsCategoryVO;
import cn.signalpha.modules.shop.wrapper.GoodsCategoryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/goods/category")
@Api(value = "商品分类", tags = "商品分类")
public class GoodsCategoryController extends BaseController {

	private final GoodsService goodsService;
	private final GoodsCategoryService goodsCategoryService;

	@GetMapping("/list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "parentId", value = "上级id", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "分类名称", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "level", value = "分类等级", paramType = "query", dataType = "string"),
	})
	@ApiOperation(value = "列表", notes = "")
	public R<List<GoodsCategory>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
		return R.data(goodsCategoryService.list(Condition.getQueryWrapper(params, GoodsCategory.class)));
	}

	@GetMapping("/lazy-list")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "parentId", value = "上级id", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "name", value = "分类名称", paramType = "query", dataType = "string"),
	})
	@ApiOperation(value = "懒加载列表", notes = "")
	public R<List<GoodsCategoryVO>> lazyList(Long parentId, @ApiIgnore @RequestParam Map<String, Object> params) {
		return R.data(GoodsCategoryWrapper.build().listNodeLazyVO(goodsCategoryService.lazyList(parentId, params)));
	}

	@GetMapping("/tree")
	@ApiOperation(value = "树形结构", notes = "")
	public R<List<GoodsCategoryVO>> tree(@ApiIgnore @RequestParam Map<String, Object> params) {
		return R.data(goodsCategoryService.tree(params));
	}

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<GoodsCategoryVO> info(@RequestParam Long id) {
		return R.data(GoodsCategoryWrapper.build().entityVO(goodsCategoryService.getById(id)));
	}

	@PostMapping("/submit")
	@ApiOperation(value = "提交", notes = "传入goodsCategory")
	public R submit(@Valid @RequestBody GoodsCategory goodsCategory) {
		if(goodsCategory.getId() == null){
			goodsCategory.setCreateTime(new Date());
		}
		goodsCategory.setUpdateTime(new Date());
		return R.status(goodsCategoryService.submit(goodsCategory));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		if (goodsCategoryService.count(Wrappers.<GoodsCategory>query().lambda().in(GoodsCategory::getParentId, Func.toLongList(ids))) > 0) {
			return R.fail("请先删除子节点!");
		}
		Func.toLongList(ids).forEach(id -> {
			if(goodsService.countByParams(Kv.create().set("categoryId", id)) > 0){
				throw new ServiceException("该分类已关联商品，请先解除绑定关系");
			}
		});
		return R.status(goodsCategoryService.removeByIds(Func.toLongList(ids)));
	}

}
